package com.cloudptt.api.product.func.call.group.fsm.call;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.log.Log;

import android.content.Context;


public class DisconnectingState implements ICallState,IRollback
{
	private GroupCallAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	public DisconnectingState(Context ctx,GroupCallAgent agent)
	{
		this.agent = agent;
		this.rollbackTimer = new RollbackTimer(ctx,1000,this);
		
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Hangup == btnAction)
		{
			int result = this.agent.doHangupAction(603);
			if(CauseCont.OperateCheck_Suc == result)
			{
				this.agent.goToState(CallConst.CallState_InitState);
//				gotoInitialState();//呼叫未成功时，松开按钮，则发cancel结束呼叫
			}
			return result;
			//			doBtnUpAction();
		}
		return CauseCont.OperateErr_Invalid;
		//		else if(GroupCallConst.BtnAction_Exit == btnAction)//和健华讨论过，挂断过程中不允许发bye
		//		{
		//			this.cancelTimer();
		//			gotoInitalState();
		//			talkAgent.doActionForTalkState(getTalkState(),GroupCallConst.BtnId_Exit);
		//		}
	}
	public void receiveCallAction(int talkAction)
	{
		if(CallConst.Session_Disconnected == talkAction)
		{
			this.agent.goToState(CallConst.CallState_InitState);
//			gotoInitialState();
		}
		//		else if(GroupCallConst.CallState_Confirmed_Active == talkAction)
		//		{
		//			//和健华讨论过，挂断过程中收到呼叫状态OK的操作，直接丢掉，不管 
		//			//talkAgent.doActionForTalkState(getTalkState(),GroupCallConst.BtnId_Exit);
		//		}
		//		else if(GroupCallConst.CallState_Confirmed_LateJoinNoFloor == talkAction//迟后接入 群组话权占用
		//				|| GroupCallConst.CallState_Confirmed_Hold == talkAction)//发起呼叫时，接到接入被叫呼叫的消息，
		//		{
		//			//和健华讨论过，挂断过程中收到呼叫状态OK的操作，直接丢掉，不管
		//			//talkAgent.doActionForTalkState(getTalkState(),GroupCallConst.BtnId_Exit);
		//		}
	}

	//	public void gotoIdleState()
	//	{
	//		ICallState currentState = new IdleState(this.talkAgent);
	//		this.talkAgent.setCurrentState(currentState);
	//	}
//	public void gotoInitialState()
//	{
//		ICallState currentState = new InitialState(this.agent);
//		this.agent.setCurrentState(currentState);
//	}



	public int getState()
	{
		return CallConst.CallState_DisconnectingState;
	}
	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}

	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "DisconnectingState rollbackState");
		if(agent != null)
		{
			this.agent.destoryCall();
			this.agent.goToState(CallConst.CallState_InitState);
//			ICallState currentState = new InitialState(this.agent);
//			this.agent.setCurrentState(currentState);
		}
	}
}
